package csc.daonjpa.java.dao;

import java.util.List;





import javax.persistence.EntityManager;
import javax.persistence.EntityTransaction;
import javax.persistence.Query;
import javax.persistence.TypedQuery;

import org.springframework.stereotype.Component;

import csc.daonjpa.java.controller.EntityManagerFactoryUtil;
import csc.daonjpa.java.domain.Account;
import csc.daonjpa.java.domain.Customer;


@Component
public class AccountDAO {

	/**
	 * @return all accounts from Account table
	 */
	public List<Account> getAccounts(String loginId) {

		// Obtains entity manager object
		EntityManager entityManager = EntityManagerFactoryUtil
				.createEntityManager();

		// Obtains transaction from entity manager
		EntityTransaction entr = entityManager.getTransaction();

		// -----------Begin transaction-----------
		List<Account> accounts = null;
		try {
			entr.begin();
			// Get a list of accounts from DB

			TypedQuery<Customer> query = entityManager.createQuery(
					"SELECT c FROM "
					+ Customer.class.getName()
					
					+ "  c where c.loginId=:id", Customer.class);

			query.setParameter("id", loginId);
			Customer a= query.getSingleResult();
			
			accounts = (List<Account>) a.getAccount();
			
			entr.commit();
		} catch (Exception e) {
			System.out.println(" " + e.getMessage() + "Get all account");
			entityManager.close();
		}
		// -----------End transaction-----------

		return accounts;

	}

	/**
	 * Add account to Account table
	 * 
	 * @param account
	 *            Account entity that contains mapped with each column in
	 *            Account table
	 * @return
	 */
	public boolean addAccount(Account account, String loginId) {
		// Obtains entity manager object
		EntityManager entityManager = EntityManagerFactoryUtil
				.createEntityManager();
		CustomerDAO cus=new CustomerDAO();
		// Obtains transaction from entity manager
		EntityTransaction entr = entityManager.getTransaction();

		try {
			// -----------Begin transaction-----------
			entr.begin();
			// Insert a row to Account table
			Customer customer = cus.getCustomerByLoginId(loginId);

			account.setCustomer(customer);

			entityManager.persist(account);

			entr.commit();
		} catch (Exception e) {
			System.out.println(" " + e.getMessage() + "Add account");
			entityManager.close();
			return false;
		}
		// -----------End transaction-----------

		return true;
	}
	
	public Account getAccountByAccno(String accountNumber) {

		// Obtains entity manager object
		EntityManager entityManager = EntityManagerFactoryUtil
				.createEntityManager();

		// Obtains transaction from entity manager
		EntityTransaction entr = entityManager.getTransaction();

		// -----------Begin transaction-----------
		Account acc = null;
		try {
			entr.begin();
			// Get a list of accounts from DB

			TypedQuery<Account> query = entityManager.createQuery(
					"SELECT c FROM " + Account.class.getName()
							+ " c Where c.accountNumber= :acc", Account.class);
			query.setParameter("acc", accountNumber);

			acc = query.getSingleResult();

			entr.commit();
		} catch (Exception e) {
			System.out.println(" " + e.getMessage() + "Get account");
			entityManager.close();
		}
		// -----------End transaction-----------

		return acc;

	}
	
	public boolean checkUpdateAccount(int available, String accountNumber) {

		// Obtains entity manager object
		EntityManager entityManager = EntityManagerFactoryUtil
				.createEntityManager();

		// Obtains transaction from entity manager
		EntityTransaction entr = entityManager.getTransaction();

		// -----------Begin transaction-----------

		boolean check = false;
		int updateCount = 0;
		try {
			entr.begin();

			String sqlQuery = "UPDATE "
					+ Account.class.getName()
					+ " c SET c.availableNumber= :avai WHERE c.accountNumber= :acc";
			Query query = entityManager.createQuery(sqlQuery);
			query.setParameter("avai", available);
			query.setParameter("acc", accountNumber);

			updateCount = query.executeUpdate();
			if (updateCount > 0) {
				check = true;
			}
			entr.commit();
		} catch (Exception e) {
			System.out.println("update Account " + e.getMessage());
			entityManager.close();
		}
		// -----------End transaction-----------

		return check;

	}
}
